export function deepCopy(target){ 
    let copyed_objs = [];//此数组解决了循环引用和相同引用的问题，它存放已经递归到的目标对象 
        function _deepCopy(target){ 
            if((typeof target !== 'object')||!target){return target;}
            for(let i= 0 ;i<copyed_objs.length;i++){
                if(copyed_objs[i].target === target){
                    return copyed_objs[i].copyTarget;
                }
            }
            let obj = {};
            if(Array.isArray(target)){
                obj = [];//处理target是数组的情况 
            }
            copyed_objs.push({target:target,copyTarget:obj}) 
            Object.keys(target).forEach(key=>{ 
                if(obj[key]){ return;} 
                obj[key] = _deepCopy(target[key]);
            }); 
            return obj;
        } 
        return _deepCopy(target);
}

// 生成笛卡尔积
export function cartesian(elements) {
    if (!Array.isArray(elements))
        throw new TypeError();
    let end = elements.length - 1,
        result = [];
  
    function addTo(curr, start) {
        let first = elements[start],
            last = (start === end);
        for (let i = 0; i < first.length; ++i) {
            let copy = curr.slice();
            copy.push(first[i]);
            if (last) {
                result.push(copy);
            } else {
                addTo(copy, start + 1);
            }
        }
    }
  
    if (elements.length)
        addTo([], 0);
    else
        result.push([]);
    return result;
  }


//   id数组排序且字符拼接
export function getSortNumberStr(arr){
    // console.log(arr)
    return arr.sort(sortNumber).map((item)=>{ return item.join('-')})
}
  

export function sortNumber(a,b){
  return a - b
}

export function  merge(ids,values){
    let key = getSortNumberStr(cartesian(ids))//id笛卡尔积
    let new_values = cartesian(values)//规格值笛卡尔积
    // console.log(key,new_values)
    if(ids.length == values.length){
       return key.map((item,index)=>{
         let spce_value_id = { }
         let name = key[index] 
         spce_value_id[name] = new_values[index];
         return spce_value_id
       })
    }else{
      return []
    }
}

// 原生返回数据
export function createTableData(arr,child_name="child"){
    /**
     * 数组格式：[{ id,title, child:[ { id,title, spec_title } ] } ],
     * 例如：[ { id:1,title:'颜色'，child:[ { id:13,title:'白色'，spec_title:'颜色' }] } ]
    **/
    let ids = [];
    let values = [];
    arr.forEach((item,index)=>{
        ids.push(item[child_name].map((val)=>{ return val.id }))
        values.push(item[child_name].map((val)=>{ return val.title }))
    })
    /**
     * ids:[[1,344,33]]为规格值id的数组集合，如颜色值白色，蓝色等代表的id数组集合
     * values:[['白色','蓝色','绿色']],为规格值数组名称集合
     * ids与values值要一一对应
     */
    let sku_values = cartesian(values);
    let sku_ids = cartesian(ids);
    /**
     * 此处生成规格值与规格名的笛卡尔积
     */
    let t = sku_values.map((val,index)=>{
        // let sku_name = val.map((v,k)=>{ return { key:{ id:arr[k].id,value:arr[k].title },value:{ id:sku_ids[index][k],value:v } } })
        let sku_name = val.map((v,k)=>{ return arr[k].title+":"+v})
        return {
            sku_name,
            picture:'',
            stock:999999,
            market_price:'',
            cost_price:'',
            status:1,
            give_point:0,
            price:'',
            product_weight:0,
            stock_warning:1,
            spec_id:sku_ids[index].sort(sortNumber).join('-')
        }
    })
    /**
     * 此处根据规格值与规格名笛卡尔积组合生成笛卡尔积表格，返回生成笛卡尔积数组模式
     */
    return t
}

function sku_create(arr){
    return arr.map((item)=>{
        return {
            inputVisible:false,
            inputValue:'',
            id:item[0].spec_id,
            key:item[0].spec_title,
            value:item.map((val)=>{ return val.title }),
            origin:item
        }
    })
}


function  merge1(ids,values,origin){
    // console.log(origin)
    // console.log(sku_create(origin))
    let key = getSortNumberStr(cartesian(ids))//id笛卡尔积
    let new_values = cartesian(values)//规格值笛卡尔积
    // console.log(key,new_values)
    if(ids.length == values.length){
       return key.map((item,index)=>{
         let spce_value_id = { }
         let name = key[index] 
         spce_value_id[name] = new_values[index];
         return {
             key:name,
             value:new_values[index]
         }
       })
    }else{
      return []
    }
}


// 对象转数组
export function ObjectToArray(object){
    let arr = [];
    for (const key in object) {
        arr.push({
            key:key,
            value:object[key]
        })
    }
    return arr
}

// 数组去重
export function unique(arr,key){
	if(!Array.isArray(arr)){
		console.log('type error!')
		return
	}
	let obj = {}
	let array = arr.reduce((init,currentValue,index,arr)=>{
		obj[currentValue.key] ? "" : obj[currentValue.key] = true && init.push(currentValue);
		return init
	},[])
	return array;
}
